package com.yq.servlet;


import com.sun.javafx.image.IntPixelGetter;
import com.yq.bean.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/addCard")
public class ServletAddCard extends HttpServlet {



    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession();
        String id = request.getParameter("id");
        ArrayList<String> ids = (ArrayList<String>) session.getAttribute("cards");
        if (ids==null){
            ids = new ArrayList<>();
            session.setAttribute("cards",ids);
        }
        ids.add(id);
        response.getWriter().println("加入购物车成功，即将返回首页！");
//        response.sendRedirect(request.getContextPath()+"/index");
        response.setHeader("refresh","2;url="+request.getContextPath()+"/index");
    }
}
